package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

public class MuriPerimetraliObject extends JPanel {

	private int count = 0;
	private String[] elPiano = { "", "5°", "4°", "3°", "2°", "1°", "terra",
			"-1°", "-2°", "-3°", "-4°", "-5°", "altro" };
	private JComboBox piano = new JComboBox(elPiano);
	private boolean modPiano = false;
	private JTextField pianoOther = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private String[] elInterno = { " ", "tramezza in laterizio forato",
			"tramezza in laterizio porizzato", "bi-mattoni", "mattone",
			"cartongesso", "legno", "altro" };
	private String[] elIntercapedine = { " ", "Argilla espansa", "sughero",
			"polistirene", "lana di vetro", "lana di roccia", "fibra di legno",
			"poliuretano", "aria", "altro" };
	private String[] elEsterno = { " ", "C.A.", "blocchi cemento", "mattoni",
			"bi-mattoni", "poroton", "tufo", "pietra porosa", "sasso",
			"misto sassi e mattoni", "misto pietra e mattoni", "altro" };

	private JComboBox interno = new JComboBox(elInterno);
	private boolean modInterno = false;
	private JTextField internoOther = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField spessoreInterno = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JComboBox intercapedine = new JComboBox(elIntercapedine);
	private boolean modIntercapedine = false;
	private JTextField intercapedineOther = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField spessoreIntercapedine = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JComboBox esterno = new JComboBox(elEsterno);
	private boolean modEsterno = false;
	private JTextField esternoOther = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField spessoreEsterno = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private MuriPerimetraliPanel muri;
	RoundedPanelComboBox muriRound = new RoundedPanelComboBox(
			"Muri perimetrali", count, this);

	public MuriPerimetraliObject(int i, MuriPerimetraliPanel m) {
		super();
		setLayout(new MigLayout("", "[]5[]5[]5[]", "[]1[]1[]"));
		setOpaque(false);
		count = i;
		muri = m;

		muriRound.setPreferredSize(new Dimension(210, 107));
		muriRound.setOpaque(false);

		setCell();
		addAction();

		add(muriRound, "cell 0 0 1 3");
		add(new JLabel("  PIANO"), "cell 1 0");
		add(piano, "cell 1 1");
		add(interno, "cell 2 0");
		add(new JLabel("interno"), "cell 3 0");
		add(spessoreInterno, "cell 4 0");
		add(new JLabel("cm"), "cell 5 0");

		add(intercapedine, "cell 2 1");
		add(new JLabel("intercapedine"), "cell 3 1");
		add(spessoreIntercapedine, "cell 4 1");
		add(new JLabel("cm"), "cell 5 1");

		add(esterno, "cell 2 2");
		add(new JLabel("esterno"), "cell 3 2");
		add(spessoreEsterno, "cell 4 2");
		add(new JLabel("cm"), "cell 5 2");

	}

	public void remove() {
		muri.removeTabel(count);
	}

	public void setCount(int i) {
		count = i;
	}

	public void setCell() {
		interno.setPreferredSize(new Dimension(250, 35));
		intercapedine.setPreferredSize(new Dimension(250, 35));
		esterno.setPreferredSize(new Dimension(250, 35));
		internoOther.setPreferredSize(new Dimension(250, 35));
		intercapedineOther.setPreferredSize(new Dimension(250, 35));
		esternoOther.setPreferredSize(new Dimension(250, 35));
		piano.setPreferredSize(new Dimension(70, 35));
		pianoOther.setPreferredSize(new Dimension(70, 35));
		spessoreEsterno.setPreferredSize(new Dimension(70, 35));
		spessoreIntercapedine.setPreferredSize(new Dimension(70, 35));
		spessoreInterno.setPreferredSize(new Dimension(70, 35));

		internoOther.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		intercapedineOther.setBorder((BorderFactory.createEmptyBorder(4, 8, 4,
				8)));
		esternoOther.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		pianoOther.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		spessoreEsterno
				.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		spessoreIntercapedine.setBorder((BorderFactory.createEmptyBorder(4, 8,
				4, 8)));
		spessoreInterno
				.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));

		internoOther.setOpaque(false);
		intercapedineOther.setOpaque(false);
		esternoOther.setOpaque(false);
		pianoOther.setOpaque(false);
		spessoreEsterno.setOpaque(false);
		spessoreIntercapedine.setOpaque(false);
		spessoreInterno.setOpaque(false);

		interno.setFont(new Font("Arial", Font.PLAIN, 14));
		intercapedine.setFont(new Font("Arial", Font.PLAIN, 14));
		esterno.setFont(new Font("Arial", Font.PLAIN, 14));
		internoOther.setFont(new Font("Arial", Font.PLAIN, 14));
		intercapedineOther.setFont(new Font("Arial", Font.PLAIN, 14));
		esternoOther.setFont(new Font("Arial", Font.PLAIN, 14));
		piano.setFont(new Font("Arial", Font.PLAIN, 14));
		pianoOther.setFont(new Font("Arial", Font.PLAIN, 14));
		spessoreEsterno.setFont(new Font("Arial", Font.PLAIN, 14));
		spessoreIntercapedine.setFont(new Font("Arial", Font.PLAIN, 14));
		spessoreInterno.setFont(new Font("Arial", Font.PLAIN, 14));
	}

	public void addAction() {
		// action
		interno.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				if (((String) interno.getSelectedItem())
						.equalsIgnoreCase("altro")) {
					remove(interno);
					add(internoOther, "cell 2 0");
					modInterno = true;
					paintAll(getGraphics());
				}
			}
		});

		intercapedine.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				if (((String) intercapedine.getSelectedItem())
						.equalsIgnoreCase("altro")) {
					remove(intercapedine);
					add(intercapedineOther, "cell 2 1");
					modIntercapedine = true;
					paintAll(getGraphics());
				}
			}
		});

		esterno.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				if (((String) esterno.getSelectedItem())
						.equalsIgnoreCase("altro")) {
					remove(esterno);
					add(esternoOther, "cell 2 2");
					modEsterno = true;
					paintAll(getGraphics());
				}
			}
		});

		piano.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (((String) piano.getSelectedItem())
						.equalsIgnoreCase("altro")) {
					remove(piano);
					add(pianoOther, "cell 1 1");
					modPiano = true;
					paintAll(getGraphics());
				}
			}
		});

		// mouse
		MouseAdapter mlA1 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(internoOther);
					interno.setSelectedIndex(0);
					add(interno, "cell 2 0");
					modInterno = false;

					paintAll(getGraphics());
				}
			}
		};
		internoOther.addMouseListener(mlA1);

		MouseAdapter mlA2 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(intercapedineOther);
					intercapedine.setSelectedIndex(0);
					add(intercapedine, "cell 2 1");
					modIntercapedine = false;

					paintAll(getGraphics());
				}
			}
		};
		intercapedineOther.addMouseListener(mlA2);

		MouseAdapter mlA3 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(esternoOther);
					esterno.setSelectedIndex(0);
					add(esterno, "cell 2 2");
					modEsterno = false;

					paintAll(getGraphics());
				}
			}
		};
		esternoOther.addMouseListener(mlA3);

		MouseAdapter mlA4 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					remove(pianoOther);
					piano.setSelectedIndex(0);
					add(piano, "cell 1 1");
					modPiano = false;

					paintAll(getGraphics());
				}
			}
		};
		pianoOther.addMouseListener(mlA4);

	}

	public String print() {
		String print = "#" + muriRound.getPiano();
		if (modPiano)
			print = print + "\n#" + pianoOther.getText();

		else
			print = print + "\n#" + piano.getSelectedIndex();

		if (modInterno)
			print = print + "\n#" + internoOther.getText();
		else
			print = print + "\n#" + interno.getSelectedIndex();
		print = print + "\n#" + spessoreInterno.getText();

		if (modIntercapedine)
			print = print + "\n#" + intercapedineOther.getText();
		else
			print = print + "\n#" + intercapedine.getSelectedIndex();
		print = print + "\n#" + spessoreIntercapedine.getText();

		if (modEsterno)
			print = print + "\n#" + esternoOther.getText();
		else
			print = print + "\n#" + esterno.getSelectedIndex();
		print = print + "\n#" + spessoreEsterno.getText();

		return print;
	}

	public void setElement(String data) {
		// System.out.println("@@ "+data);
		Scanner c = new Scanner(data);
		// c.nextLine();
		String tmp = c.nextLine();

		try {
			muriRound.setPiano(Integer.parseInt(tmp));

		} catch (NumberFormatException e) {

		}
		tmp = c.nextLine();
		try {
			piano.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			pianoOther.setText(tmp);
			remove(piano);
			add(pianoOther, "cell 1 1");
			modPiano = true;
		}

		tmp = c.nextLine();
		try {
			interno.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			internoOther.setText(tmp);
			remove(interno);
			add(internoOther, "cell 2 0");
			modInterno = true;
		}
		try {
			spessoreInterno.setText(c.nextLine());
		} catch (Exception e) {
			spessoreInterno.setText("");
		}
		tmp = c.nextLine();
		try {
			intercapedine.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			intercapedineOther.setText(tmp);
			remove(intercapedine);
			add(intercapedineOther, "cell 2 1");
			modIntercapedine = true;
		}
		try {
			spessoreIntercapedine.setText(c.nextLine());
		} catch (Exception e) {
			spessoreIntercapedine.setText("");
		}
		tmp = c.nextLine();
		try {
			esterno.setSelectedIndex(Integer.parseInt(tmp));
		} catch (NumberFormatException e) {
			esternoOther.setText(tmp);
			remove(esterno);
			add(esternoOther, "cell 2 2");
			modEsterno = true;
		}
		try {
			spessoreEsterno.setText(c.nextLine());
		} catch (Exception e) {
			spessoreEsterno.setText("");
		}
	}

	

}
